import threading
import traceback

class Ctler:
    def __init__(self):
        self.running = False
        self.thread = None

    def start(self):
        self.running = True
        self.thread = threading.Thread(target=self._run, daemon=True)
        self.thread.start()
        print("Ctler started in sub thread")

    def _run(self):
        try:
            while self.running:
                print("Doing business work...")
                threading.Event().wait(5)
        except Exception as e:
            print(f"Ctler logic exception: {e}")
            traceback.print_exc()
        finally:
            self.running = False
            print("Ctler thread exited")

    def stop(self):
        self.running = False
        if self.thread and self.thread.is_alive():
            self.thread.join(timeout=3)
            if self.thread.is_alive():
                print("Business logic thread did not exit in time, forcefully terminating")
                self.thread._stop()